package com.smarthealthring.mm.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.smarthealthring.mm.R;
import com.smarthealthring.mm.utils.SizeUtils;

/**
 * Created by hewei26 on 2018/4/8.
 */

public class SingleItemView extends RelativeLayout {

    private ImageView mIvLeft;
    private TextView mTvTitle;
    private TextView mTvSubTitle;
    private TextView mTvRight;
    private ImageView mIvRight;

    public SingleItemView(Context context) {
        super(context);
    }

    public SingleItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.SingleItemViews);
        int ivLeft = mTypedArray.getResourceId(R.styleable.SingleItemViews_iv_left, R.drawable.power);
        int ivRight = mTypedArray.getResourceId(R.styleable.SingleItemViews_iv_right, R.drawable.power);
        String strTitle = mTypedArray.getString(R.styleable.SingleItemViews_tv_title);
        String steSubTitle = mTypedArray.getString(R.styleable.SingleItemViews_tv_subtitle);
        String strRight = mTypedArray.getString(R.styleable.SingleItemViews_tv_right);
        boolean substitleVisible = mTypedArray.getBoolean(R.styleable.SingleItemViews_subtitle_visible, true);
        boolean ivLeftVisible = mTypedArray.getBoolean(R.styleable.SingleItemViews_iv_left_visible, true);
        float textSize = mTypedArray.getDimension(R.styleable.SingleItemViews_tv_right_size, SizeUtils.sp2px(context, 15));

        int rightType = mTypedArray.getInt(R.styleable.SingleItemViews_rightstyle, 0);


        initView(context);
        mTvTitle.setText(strTitle);
        mTvSubTitle.setText(steSubTitle);
        mTvSubTitle.setVisibility(substitleVisible ? View.VISIBLE : View.GONE);
        mIvLeft.setVisibility(ivLeftVisible ? View.VISIBLE : View.GONE);
        mIvLeft.setImageResource(ivLeft);
        if (rightType == 0) {
            mTvRight.setVisibility(View.VISIBLE);
            mTvRight.setText(strRight);
        } else if (rightType == 1) {
            mIvRight.setVisibility(View.VISIBLE);
            mIvRight.setImageResource(ivRight);
        } else if (rightType == 2) {
            mTvRight.setVisibility(View.VISIBLE);
            mTvRight.setText(strRight);
            mIvRight.setVisibility(View.VISIBLE);
            mIvRight.setImageResource(ivRight);
        }

//        mIvRight.setOnClickListener(new OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                mListener.onRightImgClick();
//            }
//        });
        mIvRight.setClickable(false);

        mTypedArray.recycle();
    }

    private void initView(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.view_single_item, this);
        mIvLeft = view.findViewById(R.id.iv_left);
        mTvTitle = view.findViewById(R.id.tv_title);
        mTvSubTitle = view.findViewById(R.id.tv_subtitle);
        mTvRight = view.findViewById(R.id.tv_right);
        mIvRight = view.findViewById(R.id.iv_right);
    }

    public void setmTvRight(String s) {
        if (mIvRight.getVisibility() == View.VISIBLE) {
//            mIvRight.setVisibility(View.GONE);
            mIvRight.setClickable(false);
            mTvRight.setVisibility(VISIBLE);
        }

        mTvRight.setText(s);
    }

    public void setmTvSubTitle(String s) {
        if (mTvSubTitle.getVisibility() == View.VISIBLE) {
            mTvSubTitle.setVisibility(View.GONE);
            mTvSubTitle.setVisibility(VISIBLE);
        }
        mTvSubTitle.setText(s);
    }

    public String getRightText() {
        return mTvRight.getText().toString().trim();
    }


    public interface RightImgClickListener{
        void onRightImgClick();
    }

    private RightImgClickListener mListener;
    public void setRightImgClickListener(RightImgClickListener listener) {
        mListener = listener;
    }
}
